﻿namespace Sharpmark.Tools.ProjectStyler
{
    using System;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;

    public partial class ProjectFormatter : Form
    {
        private FileEncoder _encoder;

        public ProjectFormatter()
        {
            InitializeComponent();

            _encoder = new FileEncoder(Encoding.Default)
                           {
                               ExtendFileNames =
                                   new[]
                                       {
                                           "resx", "settings", "txt", // misc files
                                           "csproj", "sln", "vcproj", // project files
                                           "cpp", "h", "cs"           // source code
                                       }
                           };
        }

        private void btnSourceDirectory_Click(object sender, EventArgs e)
        {
            if (txtSourceDirectory.Text != "")
                dlgFolderBrower.SelectedPath = txtSourceDirectory.Text;

            dlgFolderBrower.ShowNewFolderButton = false;
            if (dlgFolderBrower.ShowDialog() == DialogResult.OK)
            {
                txtSourceDirectory.Text = dlgFolderBrower.SelectedPath;

                RefreshItemsList();

                CheckDirectoryInfo();
            }
        }

        private void RefreshItemsList()
        {
            clbFormattedFiles.Items.Clear();
            clbFilesTobeFormat.Items.Clear();

            var formattedfiles = _encoder.GetFormattedFiles();
            var newfiles = _encoder.GetNewFilesToBeFormat(txtSourceDirectory.Text, formattedfiles);

            foreach (var f in formattedfiles)
            {
                clbFormattedFiles.Items.Add(f, false);
            }

            foreach (var f in newfiles)
            {
                clbFilesTobeFormat.Items.Add(f, true);
            }
        }

        private void btnDestinationDirectory_Click(object sender, EventArgs e)
        {
            if (txtDestinationDirectory.Text != "")
                dlgFolderBrower.SelectedPath = txtDestinationDirectory.Text;
            dlgFolderBrower.ShowNewFolderButton = true;
            if (dlgFolderBrower.ShowDialog() == DialogResult.OK)
            {
                txtDestinationDirectory.Text = dlgFolderBrower.SelectedPath;

                CheckDirectoryInfo();
            }
        }

        private void CheckDirectoryInfo()
        {
            if (Directory.Exists(txtDestinationDirectory.Text) &&
                Directory.Exists(txtSourceDirectory.Text))
            {
                btnFormat.Enabled = true;
            }
            else
            {
                btnFormat.Enabled = false;
            }

        }

        private void ProjectFormatterForm_Load(object sender, EventArgs e)
        {
            CheckDirectoryInfo();
        }

        private void btnFormat_Click(object sender, EventArgs e)
        {
            pgbFormatProgress.Maximum = clbFormattedFiles.CheckedItems.Count +
                                        clbFilesTobeFormat.CheckedItems.Count;

            btnFormat.Enabled = false;
            btnFormat.Text = "Formatting...";

            foreach (string item in clbFormattedFiles.CheckedItems)
            {
                _encoder.EncodingToUTF8(item, item.Replace(
                    txtSourceDirectory.Text,
                    txtDestinationDirectory.Text));

                pgbFormatProgress.Value++;
            }

            foreach (string item in clbFilesTobeFormat.CheckedItems)
            {
                _encoder.EncodingToUTF8(item, item.Replace(
                    txtSourceDirectory.Text,
                    txtDestinationDirectory.Text));
                pgbFormatProgress.Value++;
            }

            RefreshItemsList();

            btnFormat.Text = "Done.";
            btnFormat.Enabled = true;
        }
    }
}
